<?php
namespace app\admin\controller;

class Wechat extends Base
{
    public function index()
    {
        if (input("get.wechat_name")) {
            $where["wechat_name"] = ["like","%".input("get.wechat_name")."%"];
            $this->assign("wechat_name",input("get.wechat_name"));
        }else{
            $this->assign("wechat_name",'');
        }
        $where["id"] = ["gt",0]; 
    	$count = db('wechat_site')->where($where)->count();
    	$page = new \think\Page($count,10);
		$list = db('wechat_site')->where($where)->order("id desc")->limit($page->firstRow.','.$page->listRows)->select();
        foreach ($list as $key => $value) {
            $list[$key]["url"] = config("afs.server_name").'/event/wechat/active.html?wechat='.$value["id"];
        }
		$page = $page->show();
		$this->assign('list', $list);
		$this->assign('page', $page);
		
		return $this->fetch();
    }

    public function add()
    {
        if (request()->isPost()) {
            $add['wechat_name'] = input("post.wechat_name");
            if (strpos($_FILES["image"]["type"],"image") !== false) {
                // || in_array(pathinfo($_FILES["thum_img"]["name"],PATHINFO_EXTENSION), ["png", "jpg", "jpeg", "gif", "bmp"])
                $add['image'] = fileUpLoad($_FILES["image"]);
            }
            $add['content'] = input("post.content");
            $add['appid'] = input("post.appid");
            $add['appsecret'] = input("post.appsecret");
            $add['token'] = input("post.token");

            if (empty($add["wechat_name"]) || empty($add["appid"]) || empty($add["appsecret"])) {
                $this->error('所有内容不得为空！');
            }else{
                $back = db('wechat_site')->insert($add);
                if ($back !== false) {
                    $this->success('小程序添加成功', '/admin/wechat.html');
                }else{
                    $this->error('小程序添加失败');
                }
            }
        }
        return $this->fetch();
    }

    public function update()
    {

        $id = input("get.id");

        if (request()->isPost()) {
            $update['wechat_name'] = input("post.wechat_name");
            if (strpos($_FILES["image"]["type"],"image") !== false) {
                $update['image'] = fileUpLoad($_FILES["image"]);
            }
            $update['content'] = input("post.content");
            $update['appid'] = input("post.appid");
            $update['appsecret'] = input("post.appsecret");
            $update['token'] = input("post.token");
            if (empty($update["wechat_name"]) || empty($update["appid"]) || empty($update["appsecret"])) {
                $this->error('所有内容不得为空！');
            }else{
                $back = db('wechat_site')->where("id = ".$id)->update($update);
                if ($back) {
                    $this->success('小程序修改成功', '/admin/wechat.html');
                }else{
                    $this->error('小程序修改失败');
                }
            }
        }

        if ($id ) {
            $wechat = db('wechat_site')->where("id = ".$id)->find();
        }
        $this->assign("wechat",$wechat);
        return $this->fetch();
    }

    public function delete()
    {
        $id = input("get.id");
        $where["id"] = ["in",$id];
        $back = db('wechat_name')->where($where)->delete();
        if ($back !== false) {
            $array = ["status" => 1,"message" => "删除成功"];
        }else{
            $array = ["status" => 0,"message" => "删除失败"];
        }
        return json($array);
    }


    public function mknav()
    {
        $wechat_id = input("get.id");
        $where["wechat_id"] = $wechat_id;
        $where["parent_id"] =0;
        $wechat = db('wechat_nav')->where($where)->select();
        foreach ($wechat as $key => $value) {
            $where["wechat_id"] = $wechat_id;
            $where["parent_id"] = $value["id"];
            $children = db('wechat_nav')->where($where)->select();
            
            if ($children) {
                $wechat[$key]["children"] = array_reverse($children);
            }
        }
        $postData = [];
        $postData["button"] = [];
        foreach ($wechat as $key => $value) {
            if (isset($value["children"])) {
                $cache["name"] = $value["name"];
                $cache["sub_button"] = [];
                foreach ($value["children"] as $k => $v) {
                    $cache["sub_button"][] = json_decode($v["info"],true);
                }
                $postData["button"][] = $cache;
            }else{
                $postData["button"][] = json_decode($value["info"],true);
            }
        }
        $data = json_encode($postData,JSON_UNESCAPED_UNICODE);
        import('org.wechat');
        $wechat = new \Wechat();
        // dump($postData);
        $back = $wechat->mkNav($wechat_id,$data);
        $back = json_decode($back,true);
        dump($back);
        if ($back["errmsg"] == 'ok') {
             $this->success('修改成功', '/admin/wechat/nav.html');
        }else{
            $this->error('修改失败');
        }
    }

    public function nav()
    {

        if (input("get.name")) {
            $where["n.name"] = ["like",'%'.input("get.name").'%'];
            $this->assign("name",input("get.name"));
        }else{
            $this->assign("name",'');
        }
        
        if (input("get.wechat_id")) {
            $where["n.wechat_id"] = input("get.wechat_id");
            $this->assign("wechat_id",input("get.wechat_id"));
        }else{
            $this->assign("wechat_id",0);
        }

        $where["n.id"] = ["gt",0]; 
        $count = db('wechat_nav n')->where($where)->count();
        $page = new \think\Page($count,10);
        $list = db('wechat_nav n')->field("n.*,s.wechat_name")->join("afs_wechat_site s","s.id = n.wechat_id")->where($where)->order("n.id desc")->limit($page->firstRow.','.$page->listRows)->select();
        $ids = null;
        foreach ($list as $key => $value) {
            if (empty($ids)) {
                $ids = $value["parent_id"];
            }else{
                $ids .= ','.$value["parent_id"];
            }
        }
        $where1["id"] = ["in",$ids];
        $nav = db('wechat_nav')->where($where1)->select();
        foreach ($nav as $key => $value) {
            $end[$value["id"]] = $value["name"];
        }
        
        foreach ($list as $key => $value) {
            $list[$key]["parent_name"] = isset($end[$value["parent_id"]]) ? $end[$value["parent_id"]] : "无";
        }
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        
        $wechat = db("wechat_site")->select();
        $this->assign("wechat",$wechat);

        return $this->fetch();
    }

    public function navadd()
    {

        if (request()->isAjax()) {
            $where["wechat_id"] = input("post.wechat_id");
            $where["parent_id"] = 0;
            $nav = db("wechat_nav")->where($where)->select();
            $end = [];
            foreach ($nav as $key => $value) {
                $end[$value["id"]] = $value["name"];
            }
            return json($end);
        }

        if (request()->isPost()) {
            $post = input("post.");
            $add["wechat_id"] = $post["wechat_id"];
            $add["name"] = $post["name"];
            $add["parent_id"] = $post["parent_id"];
            $info = $post;
            unset($info["wechat_id"]);
            unset($info["parent_id"]);
            unset($info["grade"]);
            $add["info"] = json_encode($info,JSON_UNESCAPED_UNICODE);
            if (empty($add["wechat_id"]) || empty($add["name"]) || empty($add["info"])) {
                $this->error('所有内容不得为空！');
            }else{
                $back = db("wechat_nav")->insert($add);
                if ($back !== false) {
                    $this->success('添加成功', '/admin/wechat/nav.html');
                }else{
                    $this->error('添加失败');
                }
            }
        }
        $wechat = db("wechat_site")->select();
        $this->assign("wechat",$wechat);
        return $this->fetch();
    }

    public function navupdate()
    {
        $id = input("get.id");
        if (request()->isAjax()) {
            $where["wechat_id"] = input("post.wechat_id");
            $where["parent_id"] = 0;
            $nav = db("wechat_nav")->where($where)->select();
            $end = [];
            foreach ($nav as $key => $value) {
                $end[$value["id"]] = $value["name"];
            }
            return json($end);
        }

        if (request()->isPost()) {
            $post = input("post.");
            $update["wechat_id"] = $post["wechat_id"];
            $update["name"] = $post["name"];
            $update["parent_id"] = $post["parent_id"];
            $info = $post;
            unset($info["wechat_id"]);
            unset($info["parent_id"]);
            unset($info["grade"]);
            $update["info"] = json_encode($info,JSON_UNESCAPED_UNICODE);
            if (empty($update["wechat_id"]) || empty($update["name"]) || empty($update["info"])) {
                $this->error('所有内容不得为空！');
            }else{
                $back = db("wechat_nav")->where("id = ".$id)->update($update);
                if ($back !== false) {
                    $this->success('添加成功', '/admin/wechat/nav.html');
                }else{
                    $this->error('添加失败');
                }
            }
        }
        $wechat = db("wechat_site")->select();
        $this->assign("wechat",$wechat);
        return $this->fetch();
    }

    public function navdelete()
    {
        $id = input("get.id");
        $where["id"] = ["in",$id];
        $back = db('wechat_nav')->where($where)->delete();
        if ($back !== false) {
            $array = ["status" => 1,"message" => "删除成功"];
        }else{
            $array = ["status" => 0,"message" => "删除失败"];
        }
        return json($array);
    }

    public function autoreply()
    {
        if (input("get.title")) {
            $where["a.title"] = ["like","%".input("get.title")."%"];
            $this->assign("title",input("get.title"));
        }else{
            $this->assign("title",'');
        }

        if (input("get.wechat_id")) {
            $where["a.wechat_id"] = input("get.wechat_id");
            $this->assign("wechat_id",input("get.wechat_id"));
        }else{
            $this->assign("wechat_id","0");
        }

        $where["a.id"] = ["gt",0]; 
        $count = db("wechat_autoreplay a")->field("a.*,s.wechat_name")->join("afs_wechat_site s","s.id = a.wechat_id")->where($where)->count();
        $page = new \think\Page($count,10);
        $list = db("wechat_autoreplay a")->field("a.*,s.wechat_name")->join("afs_wechat_site s","s.id = a.wechat_id")->where($where)->order("a.id desc")->limit($page->firstRow.','.$page->listRows)->select();
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $wechat = db("wechat_site")->field("id,wechat_name")->select();
        $wechat_event_config = config("afs.wechat_event_config");
        $this->assign("wechat_event_config",$wechat_event_config);
        $wechat_autoreply_config = config("afs.wechat_autoreply_config");
        $this->assign("wechat_autoreply_config",$wechat_autoreply_config);
        $this->assign("wechat",$wechat);
        return $this->fetch();
    }

    public function autoreplyadd()
    {
        if (request()->isPost()) {
            $add['wechat_id'] = input("post.wechat_id");
            $add['event'] = input("post.event");
            $add['autoreply'] = input("post.autoreply");
            $add['title'] = input("?post.title") ? input("post.title") : '';
            $add['keywords'] = input("?post.keywords") ? input("post.keywords") : '';
            $add['description'] = input("?post.description") ? input("post.description") : '';
            $add['img_url'] = input("?post.img_url") ? input("post.img_url") : '';
            $add['article_url'] = input("?post.article_url") ? input("post.article_url") : '';
            
            if (isset($_FILES["img_url"]["type"])) {
                if (strpos($_FILES["img_url"]["type"],"image") !== false) {
                    $add['img_url'] = fileUpLoad($_FILES["img_url"]);
                }
            }

            if (empty($add["wechat_id"]) || empty($add["event"]) || empty($add["autoreply"])) {
                $this->error('所有内容不得为空！');
            }else{
                $back = db('wechat_autoreplay')->insert($add);
                if ($back !== false) {
                    $this->success('添加成功', '/admin/wechat/autoreply.html');
                }else{
                    $this->error('添加失败');
                }
            }
        }
        $wechat_event_config = config("afs.wechat_event_config");
        $this->assign("wechat_event_config",$wechat_event_config);
        $wechat_autoreply_config = config("afs.wechat_autoreply_config");
        $this->assign("wechat_autoreply_config",$wechat_autoreply_config);
        $wechat = db("wechat_site")->field("id,wechat_name")->select();
        $this->assign("wechat",$wechat);
        return $this->fetch();
    }

    public function autoreplyupdate()
    {
        $id = input("get.id");
        if (request()->isPost()) {
            $update['wechat_id'] = input("post.wechat_id");
            $update['event'] = input("post.event");
            $update['autoreply'] = input("post.autoreply");
            $update['title'] = input("?post.title") ? input("post.title") : '';
            $update['keywords'] = input("?post.keywords") ? input("post.keywords") : '';
            $update['description'] = input("?post.description") ? input("post.description") : '';
            $update['img_url'] = input("?post.img_url") ? input("post.img_url") : '';
            $update['article_url'] = input("?post.article_url") ? input("post.article_url") : '';
            
            if (isset($_FILES["img_url"]["type"])) {
                if (strpos($_FILES["img_url"]["type"],"image") !== false) {
                    $update['img_url'] = fileUpLoad($_FILES["img_url"]);
                }
            }

            if (empty($update["wechat_id"]) || empty($update["event"]) || empty($update["autoreply"])) {
                $this->error('所有内容不得为空！');
            }else{
                $back = db('wechat_autoreplay')->where("id = ".$id)->update($update);
                if ($back !== false) {
                    $this->success('修改成功', '/admin/wechat/autoreply.html');
                }else{
                    $this->error('修改失败');
                }
            }
        }
        $wechat_event_config = config("afs.wechat_event_config");
        $this->assign("wechat_event_config",$wechat_event_config);
        $wechat_autoreply_config = config("afs.wechat_autoreply_config");
        $this->assign("wechat_autoreply_config",$wechat_autoreply_config);
        $wechat = db("wechat_site")->field("id,wechat_name")->select();
        $this->assign("wechat",$wechat);
        $autoreplay = db('wechat_autoreplay')->where("id = ".$id)->find();
        $this->assign("autoreplay",$autoreplay);
        return $this->fetch();
    }

    public function autoreplydelete()
    {
        $id = input("get.id");
        $where["id"] = ["in",$id];
        $back = db('wechat_autoreplay')->where($where)->delete();
        if ($back !== false) {
            $array = ["status" => 1,"message" => "删除成功"];
        }else{
            $array = ["status" => 0,"message" => "删除失败"];
        }
        return json($array);
    }


}
